package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Jeanyung
 */
@Service
public class NursingProjectServiceimpl  implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询
     */
    @Override
    public PageResponse<NursingProjectVo> pageQuery(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
     Page<NursingProjectVo> page= nursingProjectMapper.pageQuery(name,status);
        return PageResponse.of(page, NursingProjectVo.class) ;
    }

    /**
     * 新增护理项目
     * @param dto
     */
    @Override
    public void addNursingProject(NursingProjectDto dto) {
        //dto转entity
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        //判断护理项目名字是否重复
     Integer count=  nursingProjectMapper.selectByNursingProjectName(dto.getName());
     if(count>0){
         throw new BaseException(BasicEnum.NURSIGN_PROJECT_EXIST);
     }
     //调用新增mapper
        nursingProjectMapper.insertNursingProject(nursingProject);
    }

    /**
     * 回显
     * @param id
     */
    @Override
    public NursingProjectVo getById(Long id) {
       NursingProject nursingProject= nursingProjectMapper.selectNursingProjectById(id);
        NursingProjectVo vo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return vo;
    }

    /**
     * 更新护理项目
     * @param dto
     */
    @Override
    public void updateNursingProject(NursingProjectDto dto) {
        //dto转entity
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        //判断护理项目名字是否重复
        Integer count=  nursingProjectMapper.selectByNursingProjectNameForUpdate(dto.getId());
        if(count>0){
            throw new BaseException(BasicEnum.NURSIGN_PROJECT_EXIST);
        }
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    /**
     * 删除护理项目
     * @param id
     */
    @Override
    public void deleteNursingProject(Long id) {
      Integer count=  nursingProjectMapper.selectByNursingProjectNameForUpdate(id);
        if (count>0){
            throw new BaseException(BasicEnum.DELETE_HULIJIHUA);
        }
       NursingProject nursingProject= nursingProjectMapper.selectNursingProjectById(id);
        if (nursingProject==null){
            throw new BaseException(BasicEnum.NURSIGN_PROJECT_NOEXIST);
        }
        nursingProjectMapper.deleteNursingProjectById(id);
    }

    /**
     * 启用/禁用
     * @param id
     * @param status
     */
    @Override
    public void statusNursingProject(Long id, Integer status) {
        nursingProjectMapper.statusNursingProject(id,status);
    }



    /**
     * 列出所有护理项目
     */
    @Override
    public List<NursingProjectVo> getAllNursingProject() {
        List<NursingProjectVo> list =nursingProjectMapper.getAllNursingProject();
        return list;
    }

}
